<?php
class RuleController extends AdminController {
    public function listAction(){
        $model = new UserRuleModel;
        $this->_view->rules = $model->select();
    }
    public function editAction(){
        $model = new UserRuleModel;
        $this->_view->rule = $model->getById();
    }
    public function updateAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        if($this->_request->isPost()){
            $rule_id = Request::aspost(3,'id');
            if($rule_id === null){
                exit;
            }else{
                $model = new UserRuleModel;
                $flag = $model->up($rule_id);
                if($flag){
                    Response::success('Nice!');
                }else{
                    Response::error('Wrong');
                }
            }
        }else{
            exit;
        }        
    }
    public function createAction(){}
    public function insertAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        if($this->_request->isPost()){
            $model = new UserRuleModel;
            $flag = $model->add();
            if($flag){
                Response::success('Nice!');
            }else{
                Response::error('Wrong!');
            }
        }else{
            exit;
        }
    }
    public function statusAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        $id = Request::aspost(3,'id','');
        if(empty($id)){
            die;
        }else{
            // Mark... Filter The 'status'
            $data['rule_status']= isset($_POST['status']) ? $_POST['status'] : '';

            if($data['rule_status'] == ''){
                return false;
            }else{
                $model = new UserRuleModel;
                $flag = $model->where(['rule_id'=>$id])->update($data);
                if($flag == 0 || $flag == false){
                    Response::error('error');
                }else{
                    Response::success('nice');
                }
            }
        }        
    }        
    public function removeAction(){
        Yaf_Dispatcher::getInstance()->disableView();
        if($this->_request->isPost()){
            $rule_id = Request::aspost(3,'id');
            if($rule_id === null){
                exit;
            }else{
                $model = new UserRuleModel;
                $flag = $model->delete($rule_id);
                if($flag){
                    Response::success('wrong');
                }else{
                    Response::error('wrong');
                }
            }
        }else{
            exit;
        }           
    }
}